package com.baox.agridata.synchronous.service.impl;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baox.agridata.synchronous.entity.*;
import com.baox.agridata.synchronous.result.Result;
import com.baox.agridata.synchronous.service.*;
import com.baox.agridata.synchronous.util.DateUtil;
import com.baox.agridata.synchronous.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.security.cert.Certificate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class AsyncCertificateServiceImpl implements AsyncCertificateService {


    @Autowired
    private TCCABService tccabService;

    @Autowired
    private TCCAAService tccaaService;

    @Autowired
    private TCCPBService tccpbService;

    @Autowired
    private TCCPAService tccpaService;


    @Autowired
    private TCCOutAAService tccOutAAService;

    @Autowired
    private TCCOutPAService tccOutPAService;


    //----------------------------------这是处理检疫证的开始------------------------------------------

    @Override
    public Result dealReceiveListAnimalBData(List<TCCABEntity> l_list) {
        try {
            long start = System.currentTimeMillis();
            Integer insertCount = 0, updateCount = 0;
            StringBuilder sb = new StringBuilder();
            sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
            for (TCCABEntity data : l_list) {
                TCCABEntity tccabEntity = tccabService.selectTCCABById(data.getMid());
                if (tccabEntity == null) {
                    data.setSourceId(sb.toString());
                    insertCount += tccabService.insertIntoTCCAB(data);
                } else {
                    //-------------------------reception字段
                    Integer oldReception = tccabEntity.getReception();
                    Integer newReception = data.getReception();
                    if (oldReception != null && newReception != null) {
                        int newValue = Math.max(oldReception.intValue(), newReception.intValue());
                        data.setReception(newValue);
                    }
                    //---------------------isArrived字段
                    Integer oldIsArrived = tccabEntity.getIsArrived();
                    Integer newIsArrived = data.getIsArrived();
                    if (oldIsArrived != null && newIsArrived != null) {
                        int newValue = Math.max(oldIsArrived.intValue(), newIsArrived.intValue());
                        data.setIsArrived(newValue);
                    }
                    //-------------------------status字段
                    String oldStatus = tccabEntity.getStatus();  //更新之前的status状态
                    String newStatus = data.getStatus();   //要更新的状态
                    JSONObject oldStatusJsonObj = JSON.parseObject(oldStatus);
                    Integer oldStatusKey = oldStatusJsonObj.getInteger("key");
                    JSONObject newStatusJsonObj = JSON.parseObject(newStatus);
                    Integer newStatusKey = newStatusJsonObj.getInteger("key");
                    if (newStatusKey.intValue() == 1701) {
                        data.setStatus(oldStatus);
                    } else if (newStatusKey.intValue() == 1702 || newStatusKey.intValue() == 1704) {
                        data.setStatus(newStatus);
                    } else if (newStatusKey.intValue() == 1703 && (oldStatusKey.intValue() == 1701 || oldStatusKey.intValue() == 1703)) {
                        data.setStatus(newStatus);
                    } else if (newStatusKey.intValue() == 1703 && oldStatusKey.intValue() == 1702) {
                        data.setStatus(oldStatus);
                    }
                    updateCount += tccabService.updateTCCABById(data);
                }
            }
            log.info("method dealReceiveListAnimalBData, insertCount: {},updateCount:{},total:{},总耗时：{}", insertCount, updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【AnimalB】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }


    @Override
    public Result dealReceiveListAnimalAData(List<TCCAAEntity> l_list) {
        try {
            long start = System.currentTimeMillis();
            Integer insertCount = 0, updateCount = 0;
            StringBuilder sb = new StringBuilder();
            sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
            for (TCCAAEntity data : l_list) {
                TCCAAEntity tccaaEntity = tccaaService.selectTCCAAById(data.getMid());
                if (tccaaEntity == null) {
                    data.setSourceId(sb.toString());
                    insertCount += tccaaService.insertIntoTCCAA(data);
                } else {
                    updateCount += tccaaService.updateTCCAAById(data);
                }
            }
            log.info("method dealReceiveListAnimalAData, insertCount: {},updateCount:{},total:{},总耗时：{}", insertCount, updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【AnimalA】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }


    @Override
    public Result dealReceiveListProductBData(List<TCCPBEntity> l_list) {
        try {
            long start = System.currentTimeMillis();
            Integer insertCount = 0, updateCount = 0;
            StringBuilder sb = new StringBuilder();
            sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
            List<TCCPBEntity> batchInsertList = new ArrayList<>();
            for (TCCPBEntity data : l_list) {
                TCCPBEntity tccpbEntity = tccpbService.checkTCCPBIsExist(data.getMid());
                if (tccpbEntity == null) {
                    data.setSourceId(sb.toString());
                    batchInsertList.add(data);
                } else {
                    updateCount += tccpbService.updateTCCPBById(data);
                }
            }
            if (batchInsertList.size() > 0) insertCount = tccpbService.batchInsertIntoTCCPB(batchInsertList);
            log.info("method dealReceiveListProductBData, insertCount: {},updateCount:{},total:{},总耗时：{}", insertCount, updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【ProductB】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    public Result dealReceiveListProductAData(List<TCCPAEntity> l_list) {
        try {
            long start = System.currentTimeMillis();
            Integer insertCount = 0, updateCount = 0;
            StringBuilder sb = new StringBuilder();
            sb.append(".java").append(DateUtil.dateConvertTag(new Date()));
            for (TCCPAEntity data : l_list) {
                TCCPAEntity tccpaEntity = tccpaService.selectTCCPAById(data.getMid());
                if (tccpaEntity == null) {
                    data.setSourceId(sb.toString());
                    insertCount += tccpaService.insertIntoTCCPA(data);
                } else {
                    updateCount += tccpaService.updateTCCPAById(data);
                }
            }
            log.info("method dealReceiveListProductAData, insertCount: {},updateCount:{},total:{},总耗时：{}", insertCount, updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【ProductA】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    public Result dealReceiveChengduLandingFeedbackTCCAB(List<TCCABEntity> l_list) {
        try {
            long start = System.currentTimeMillis();
            Integer updateCount = 0;
            for (TCCABEntity data : l_list) {
                TCCABEntity tccabEntity = tccabService.selectTCCABById(data.getMid());
                if (tccabEntity != null) {
                    updateCount += tccabService.updateTCCABById(data);
                }
            }
            log.info("method dealReceiveChengduLandingFeedbackTCCAB, updateCount:{},total:{},总耗时：{}", updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【LandingFeedbackTCCAB】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }

    @Override
    public Result dealReceiveChengduLandingFeedbackTCCOutAA(List<TCCOutAAEntity> l_list) {
        try {
            long start = System.currentTimeMillis();
            Integer updateCount = 0;
            for (TCCOutAAEntity data : l_list) {
                TCCOutAAEntity tccOutAAEntity = tccOutAAService.selectTCCOutAAById(data.getMid());
                if (tccOutAAEntity != null) {
                    updateCount += tccOutAAService.updateTCCOutAAById(data);
                }
            }
            log.info("method dealReceiveChengduLandingFeedbackTCCOutAA, updateCount:{},total:{},总耗时：{}", updateCount, l_list.size(), System.currentTimeMillis() - start);
        } catch (Exception e) {
            log.info("【LandingFeedbackTCCOutAA】 has exception : " + e.getMessage());
            return Result.fail();
        }
        return Result.ok();
    }


//    @Override
//    public Result dealReceiveChengduLandingFeedbackTCCPB(List<TCCPBEntity> l_list) {
//        try {
//            for (TCCPBEntity data : l_list) {
//                TCCPBEntity tccpbEntity = tccpbService.checkTCCPBIsExist(data.getMid());
//                if (tccpbEntity != null) {
//                    tccpbService.updateTCCPBById(data);
//                }
//            }
//        } catch (Exception e) {
//            log.info("【LandingFeedbackTCCPB】 has exception : " + e.getMessage());
//            return Result.fail();
//        }
//        return Result.ok();
//    }


//    @Override
//    public Result dealReceiveChengduLandingFeedbackTCCOutPA(List<TCCOutPAEntity> l_list) {
//        try {
//            for (TCCOutPAEntity data : l_list) {
//                TCCOutPAEntity tccOutPAEntity = tccOutPAService.selectTCCOutPAById(data.getMid());
//                if (tccOutPAEntity != null) {
//                    tccOutPAService.updateTCCOutAAById(data);
//                }
//            }
//        } catch (Exception e) {
//            log.info("【LandingFeedbackTCCOutAA】 has exception : " + e.getMessage());
//            return Result.fail();
//        }
//        return Result.ok();
//    }


}
